package com.example.medical.controller.admin;

import com.example.medical.entity.dto.GetListDto;
import com.example.medical.entity.pojo.Sector;
import com.example.medical.entity.pojo.StoreroomType;
import com.example.medical.service.SectorService;
import com.example.medical.utils.common.enums.Code;
import com.example.medical.utils.common.result.Result;
import com.example.medical.utils.config.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/admin/sector")
public class AdminSectorController {

    @Autowired
    private SectorService sectorService;

    @PostMapping("/get_sector_list")
    public Result AdminGetSectorList(@RequestBody GetListDto dto){
        return sectorService.AdminGetSectorList(dto);
    }

    @GetMapping("/get_sector_detail")
    public Result AdminGetSectorDetail(@RequestParam("id")Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        return Result.okResult(sectorService.getById(id));
    }

    @PostMapping("/save_sector")
    public Result AdminSaveSector(@RequestParam("name")String name){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        Sector sector=new Sector().builder().sectorName(name).saveTime(String.valueOf(LocalDateTime.now())).build();
        sectorService.save(sector);
        return Result.okResult(Code.SUCCESS);
    }

    @GetMapping("/remove_sector")
    public Result AdminRemoveSector(@RequestParam("id")Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        sectorService.removeById(id);
        return Result.okResult(Code.SUCCESS);
    }

    @GetMapping("/remove_sector_arr")
    public Result AdminRemoveSectorArr(@RequestParam("ids")String[] ids){
        return  sectorService.AdminRemoveSectorArr(ids);
    }

    @GetMapping("/update_get_sector")
    public Result AdminUpdateGetSector(@RequestParam("id")Long id){
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        return Result.okResult(sectorService.getById(id));
    }

    @PostMapping("/update_sector")
    public Result AdminUpdateSector(@RequestParam("id")Long id,
                                    @RequestParam("name")String name){
        return sectorService.AdminUpdateSector(id,name);
    }

}
